//
//  WSWelcomeController.swift
//  微博
//
//  Created by 武莎莎 on 15/9/6.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

class WSWelcomeController: UIViewController {
    // 懒加载控件
    private lazy var backgroundView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
        
    private lazy var iconView:UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 45
        iconView.clipsToBounds = true
        return iconView
    }()
    private lazy var welcomeLbl:UILabel = {
        let lbl = UILabel()
        lbl.text = "欢迎回来"
        lbl.textColor = UIColor.darkGrayColor()
        lbl.sizeToFit()
        return lbl
    }()
    private var iconBottomConstraint:NSLayoutConstraint?
    
    // MARK: 动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        let h = -(UIScreen.mainScreen().bounds.height + iconBottomConstraint!.constant)
        iconBottomConstraint?.constant = h
        // MARK: 执行动画
        welcomeLbl.alpha = 0.0
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: [], animations: { () -> Void in
            
                self.view.layoutIfNeeded()
            
            }) { (_) -> Void in

                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.welcomeLbl.alpha = 1.0
                    }, completion: { (_) -> Void in
                    
                    NSNotificationCenter.defaultCenter().postNotificationName(WSSwitchRootViewControllerNotification, object: nil)
                })
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        setUpUI()
        iconView.sd_setImageWithURL(WSUserAccountViewModel.sharedUserAccount.avatarURL)
    }
    private func setUpUI(){
        view.addSubview(backgroundView)
        view.addSubview(iconView)
        view.addSubview(welcomeLbl)
        // option + delete 删除光标左边的第一个单词
        // 背景图片
        backgroundView.ff_Fill(view)
        // 头像
        let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: self.view, size: CGSizeMake(90, 90), offset: CGPointMake(0, -200))
        iconBottomConstraint = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        // 欢迎回来
        welcomeLbl.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))

    }
}
